package com.majsubtense.main.ui;

import com.majsubtense.core.achievements.AchievementController;
import com.majsubtense.core.achievements.PressSettingButtonAchievement;
import com.majsubtense.core.components.ComponentType;
import com.majsubtense.core.components.UI;
import com.majsubtense.core.events.GameControlEvent;

import com.majsubtense.core.events.GameSceneChangeEvent;
import com.majsubtense.core.achievements.Achievement;
import com.majsubtense.main.gameScenes.Index;
import javafx.animation.FadeTransition;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.util.Duration;

import java.util.Objects;

public class IndexUI extends UI {
    public static final ComponentType<IndexUI> ANY;
    private Label gameTitle;

    private Button btn_gameStart;

    private Button btn_quit;

    private Button btn_settings;

    public IndexUI() {
        super(IndexUI.ANY);
        pane = new AnchorPane();
        scene = new Scene(pane);

        gameTitle = new Label("Racing");
        gameTitle.setFont(Font.font("Arial", FontWeight.BOLD, 50));
        gameTitle.setAlignment(Pos.CENTER);
        gameTitle.setLayoutX(183);
        gameTitle.setLayoutY(72);
        gameTitle.setPrefWidth(235);
        gameTitle.setPrefHeight(94);

        btn_gameStart = new Button("Game Start");
        btn_gameStart.setLayoutX(226);
        btn_gameStart.setLayoutY(220);
        btn_gameStart.setPrefWidth(149);
        btn_gameStart.setPrefHeight(46);
        btn_gameStart.setOnMousePressed(mouseEvent ->
                onDeliver(new GameSceneChangeEvent(GameSceneChangeEvent.ANY, "Index", "RaceVenue"))
        );

        btn_settings = new Button("Settings");
        btn_settings.setLayoutX(226);
        btn_settings.setLayoutY(276);
        btn_settings.setPrefWidth(149);
        btn_settings.setPrefHeight(46);
        btn_settings.setOnMousePressed(mouseEvent -> {
                    onDeliver(new GameSceneChangeEvent(GameSceneChangeEvent.ANY, "Index", "Settings"));

                    Label label = new Label(Objects.requireNonNull(AchievementController.getAchievement(PressSettingButtonAchievement.ANY)).getCurrentMessage());
                    label.setFont(Font.font("Arial", FontWeight.BOLD, 15));
                    FadeTransition fadeTransition = new FadeTransition(Duration.seconds(0.4), label);
                    fadeTransition.setFromValue(0.2);
                    fadeTransition.setToValue(1);
                    fadeTransition.setAutoReverse(true);
                    fadeTransition.setCycleCount(4);
                    fadeTransition.play();

                    Pane pane = (Pane) scene.getRoot();
                    pane.getChildren().add(label);

                    fadeTransition.setOnFinished(actionEvent -> pane.getChildren().remove(label));
                }
        );

        btn_quit = new Button("Quit");
        btn_quit.setLayoutX(226);
        btn_quit.setLayoutY(332);
        btn_quit.setPrefWidth(149);
        btn_quit.setPrefHeight(46);
        btn_quit.setOnMousePressed(mouseEvent ->
                onDeliver(new GameControlEvent(GameControlEvent.GAME_QUIT))
        );
    }

    @Override
    public void attach() {
        pane.getChildren().addAll(gameTitle, btn_gameStart, btn_settings, btn_quit);
    }

    @Override
    public void update() {
    }

    @Override
    public void detach() {
        pane.getChildren().removeAll(gameTitle, btn_gameStart, btn_settings, btn_quit);
    }

    static {
        ANY = new ComponentType<>(UI.ANY, "INDEX");
    }
}
